
#include "../main.h"

int bRegister = 0;
int parse_msg(int sockfd, char *msg, int length)
{
    //int i, ret;
    char *type = NULL;
    char *clientId = NULL;
    char *content = NULL;
    cJSON *pRoot = NULL, *pValue = NULL;

    int msg_size = get_size(msg);
    int data_size = get_size(msg + 4);

    int jmp_size = 2 * sizeof(int);

    char *data = charCopy(msg + jmp_size, msg_size);
    pRoot = cJSON_Parse(data);
    if (pRoot == NULL)
    {
        OUT_DEBUG(DEBUG, "JSON 数据解析失败 ! ");
        printf("json data:%s,msgsize:%d", data, msg_size);
        free(data);
        data = NULL;
        return 0;
    }
    pValue = cJSON_GetObjectItem(pRoot, "type");
    if (pValue != NULL && pValue->type == cJSON_String)
    {
        type = stringCopy(pValue->valuestring, strlen(pValue->valuestring));
    }
    pValue = cJSON_GetObjectItem(pRoot, "clientId");
    if (pValue != NULL && pValue->type == cJSON_String)
    {
        clientId = stringCopy(pValue->valuestring, strlen(pValue->valuestring));
    }
    pValue = cJSON_GetObjectItem(pRoot, "content");
    if (pValue != NULL && pValue->type == cJSON_String)
    {
        content = charCopy(pValue->valuestring, strlen(pValue->valuestring));
    }
    if (!strcmp(type, "REGISTER_SUCCESS"))
    {
        OUT_DEBUG(DEBUG, "### REGISTER_SUCCESS ###");
        //设置为已注册
        bRegister = 1;
    }
    else if (!strcmp(type, "COMMON_ERROR"))
    {
        OUT_DEBUG(DEBUG, "### COMMON_ERROR ###");
    }
    else if (!strcmp(type, "PING"))
    {
        OUT_DEBUG(DEBUG, "### PING ###");
    }
    else if (!strcmp(type, "PING_REPLY"))
    {
        OUT_DEBUG(DEBUG, "### PING_REPLY ###");
    }
    else if (!strcmp(type, "DEVICE_FILE_UPLOAD_HEAD"))
    {
        OUT_DEBUG(DEBUG, "DEVICE_FILE_UPLOAD_HEAD");
        process_up_filehead(sockfd, content, clientId);
    }
    else if (!strcmp(type, "DEVICE_FILE_UPLOAD_REQ"))
    {
        OUT_DEBUG(DEBUG, "DEVICE_FILE_UPLOAD_REQ");
        char *body = (char *)malloc(data_size);
        memset(body, 0, data_size);
        memcpy(body, msg + jmp_size + msg_size, data_size);
        process_up_file(sockfd, clientId, content, body);
        free(body);
        body = NULL;
    }
    else if (!strcmp(type, "DEVICE_FILE_DOWNLOAD_HEAD"))
    {
        process_down_head(sockfd, clientId, content);
    }
    else if (!strcmp(type, "DEVICE_FILE_DOWNLOAD_REQ"))
    {
        OUT_DEBUG(DEBUG, "DEVICE_FILE_DOWNLOAD_REQ");
        process_down_file(sockfd, clientId, content);
    }
    else if (!strcmp(type, "DEVICE_FILE_DOWNLOAD_CHECK"))
    {
        OUT_DEBUG(DEBUG, "DEVICE_FILE_DOWNLOAD_CHECK");
        process_down_file(sockfd, clientId, content);
    }
    else if (!strcmp(type, "DEVICE_FILE_RENAME_REQ"))
    {
        OUT_DEBUG(DEBUG, "DEVICE_FILE_RENAME_REQ");
        rename_file(sockfd, content);
    }
    else if (!strcmp(type, "DEVICE_FILE_DELETE_REQ"))
    {
        OUT_DEBUG(DEBUG, "DEVICE_FILE_DELETE_REQ");
        delete_file(sockfd, content);
    }
    if (pRoot != NULL)
    {
        cJSON_Delete(pRoot);
    }

    if (type != NULL)
    {
        free(type);
        type = NULL;
    }

    if (clientId != NULL)
    {
        free(clientId);
        clientId = NULL;
    }

    if (content != NULL)
    {

        free(content);
        content = NULL;
    }

    if (data != NULL)
    {
        free(data);
        data = NULL;
    }
    return 0;
}

char *get_routerid()
{
    return RouteID;
}
